﻿using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Reflection;
using Spire.Presentation;
using System.IO;
using HuiZe.education.Common;
using Label = System.Windows.Controls.Label;

namespace HuiZe.education.View
{
    /// <summary>
    /// BeginClass25.xaml 的交互逻辑
    /// </summary>
    public partial class BeginClass25 : Window
    {
        public BeginClass25()
        {
            InitializeComponent();
            //var w = new WebBrowserOverlay(this.BdBrowser);
            //var wb = w.WebBrowser;
            //var HelpUrl = "file:\\I:\\wpf_jz111\\huize-edu\\HuiZeOutPut\\ConvertHtmls\\191d0de1296c1c3b1ca34c773c3582df.html";
            //wb.Navigate(new System.Uri(HelpUrl));
            Loaded += ResourceView_Loaded;


        }
        public AttendClassViewModel attendClassView { get; set; }
        public string BkType { get; set; }
        public string BkId { get; set; }
        public ObservableCollection<ResourceCenterModel> resources { get; set; }

        private void ResourceView_Loaded(object sender, RoutedEventArgs e)
        {

            //webBrowser0 = new WebBrowser();
            //webBrowser0.ScriptErrorsSuppressed = true;
            attendClassView = DataContext as AttendClassViewModel;
            var beginResList = attendClassView.GetClassResourceDetail(BkId);
            if (beginResList != null)
            {
                resList.Children.Clear();
                foreach (var item in beginResList)
                {


                    CustomButton customButton = new CustomButton()
                    {
                        Template = (ControlTemplate)FindResource("HZImageButtonTemplate"),
                        ImageSource = new BitmapImage(new Uri(item.image_cover)),
                        SourceUrl = item.file,
                        //MeadiaType
                        Content = item.bname,
                        Width = 80,
                        Height = 40,
                        Margin = new Thickness(0, 20, 10, 0),
                    };
                    Label label = new Label
                    {
                        Content = item.bname,
                        Margin = new Thickness(0, 20, 10, 0),
                    };
                    customButton.Click += customButton_Click;


                    resList.Children.Add(customButton);
                    resList.Children.Add(label);


                }


            }




        }


        private void customButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            CustomButton customButton = sender as CustomButton;
            //string preUrl = "https://ow365.cn/?i=28004&ssl=1&n=5&furl=";
            string url = customButton.SourceUrl;
            //MediaPlayBorder.Children.Clear();


            if (url.ToString().Contains("jpg") || url.ToString().Contains("png"))
            {

                string LocalPath = DownLoadFile(url, AppDomain.CurrentDomain.BaseDirectory + "\\ConvertHtmls\\");
               
                pageContent.Content = f2;
                mediaElement.Source = new Uri(LocalPath);
                mediaElement.Play();
                //webBrowser11.Navigate("file:\\" + LocalPath);
            }
            else if (url.ToString().Contains("mp3") || url.ToString().Contains("mp4"))
            {
                string LocalPath = DownLoadFile(url, AppDomain.CurrentDomain.BaseDirectory + "\\ConvertHtmls\\");

                pageContent.Content = f2;
                mediaElement.Source = new Uri(LocalPath);
                mediaElement.Play();



            }
            else if (url.ToString().Contains("ppts") || url.ToString().Contains("ppt"))
            {
                string LocalPath = DownLoadFile(url, AppDomain.CurrentDomain.BaseDirectory + "\\ConvertHtmls\\");
                pageContent.Content = f1;
                OpenSourceFile(LocalPath);

               
            }
            else if ( url.ToString().Contains("pdf"))
            {
                //string LocalPath = DownLoadFile(url, AppDomain.CurrentDomain.BaseDirectory + "\\ConvertHtmls\\");
                //pageContent.Content = f3;
                //OpenSourceFile(LocalPath);


            }
            else if (url.ToString().Contains("doc") || url.ToString().Contains("docx"))
            {
                string LocalPath = DownLoadFile(url, AppDomain.CurrentDomain.BaseDirectory + "\\ConvertHtmls\\");
                pageContent.Content = f1;
                OpenSourceFile(LocalPath);

             
            }
            else if (url.ToString().Contains("xlsx") || url.ToString().Contains("xls"))
            {
                string LocalPath = DownLoadFile(url, AppDomain.CurrentDomain.BaseDirectory + "\\ConvertHtmls\\");
                pageContent.Content = f1;
                OpenSourceFile(LocalPath);

            }
            else
            {



            }


        }


        private void close_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //webBrowser11.Navigate("");
            this.Close();
        }



        private void SourceTree_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {


        }










        private string DownLoadFile(string url, string savepath)
        {
            string extention = Path.GetExtension(url);
            string FileName = Path.GetFileNameWithoutExtension(url);
            string downloadpath = savepath + "\\" + FileName + extention;
            WebApiHelper.Download(url, downloadpath);

            return downloadpath;


        }
     

        private void exitAddStu_Click(object sender, RoutedEventArgs e)
        {


        }

        System.Windows.Xps.Packaging.XpsDocument xpsDoc;
        public static bool officeFileOpen_Status = false;
        private void OpenSourceFile(string FilePath)
        {

            string xpsFilePath = string.Empty;

            var convertResults = OfficeToXps.ConvertToXps(FilePath, ref xpsFilePath);
            switch (convertResults.Result)
            {
                case ConversionResult.OK:
                    xpsDoc = new System.Windows.Xps.Packaging.XpsDocument(xpsFilePath, FileAccess.ReadWrite);
                    documentViewer1.Document = xpsDoc.GetFixedDocumentSequence();
                    officeFileOpen_Status = true;
                    break;

                case ConversionResult.InvalidFilePath:
                    // Handle bad file path or file missing
                    break;
                case ConversionResult.UnexpectedError:
                    // This should only happen if the code is modified poorly
                    break;
                case ConversionResult.ErrorUnableToInitializeOfficeApp:
                    // Handle Office 2007 (Word | Excel | PowerPoint) not installed
                    break;
                case ConversionResult.ErrorUnableToOpenOfficeFile:
                    // Handle source file being locked or invalid permissions
                    break;
                case ConversionResult.ErrorUnableToAccessOfficeInterop:
                    // Handle Office 2007 (Word | Excel | PowerPoint) not installed
                    break;
                case ConversionResult.ErrorUnableToExportToXps:
                    // Handle Microsoft Save As PDF or XPS Add-In missing for 2007
                    break;
            }
        }
    }
}
